<template>
	<a-modal v-model:visible="modelVisible" :title="pageData.title" draggable @cancel="resetAndClose" @before-ok="submitData">
		<a-form ref="formRef" :model="pageData.rowData" auto-label-width>
			<a-form-item
				field="teacherName"
				label="讲师姓名"
				:rules="[{ required: true, message: '讲师姓名不能为空' }]"
				:validate-trigger="['change', 'input']"
			>
				<a-input v-model="pageData.rowData.teacherName" placeholder="请输入讲师姓名" />
			</a-form-item>
			<a-form-item
				field="teacherIdCardNo"
				label="身份证号"
				:rules="[{ required: true, message: '讲师身份证号不能为空' }]"
				:validate-trigger="['change', 'input']"
			>
				<a-input v-model="pageData.rowData.teacherIdCardNo" placeholder="请输入讲师身份证号" />
			</a-form-item>
			<a-form-item
				field="agreementStartTime"
				label="协议开始时间"
				:rules="[{ required: true, message: '协议开始时间不能为空' }]"
				:validate-trigger="['change', 'input']"
			>
				<a-date-picker v-model="pageData.rowData.agreementStartTime"></a-date-picker>
			</a-form-item>
			<a-form-item
				field="agreementEndTime"
				label="协议结束时间"
				:rules="[{ required: true, message: '协议结束时间不能为空' }]"
				:validate-trigger="['change', 'input']"
			>
				<a-date-picker v-model="pageData.rowData.agreementEndTime"></a-date-picker>
			</a-form-item>
			<a-form-item field="agreementSignTime" label="协议签署时间">
				<a-date-picker v-model="pageData.rowData.agreementSignTime"></a-date-picker>
			</a-form-item>
		</a-form>
	</a-modal>
</template>

<script setup lang="ts">
import { computed, ref, reactive, watch, nextTick, onMounted } from 'vue';
import { Audit } from '@/api/types';
import { saveOrUpdate } from '@/api/audit';

const modelVisible = ref(false);
const formRef = ref();
const pageData = reactive({
	title: '',
	rowData: {},
});

interface ModelProps {
	visible: boolean;
	rowData: Audit.Teacher;
}

interface ModelEmits {
	(e: 'successFunc'): void;
}

const emit = defineEmits<ModelEmits>();

// 接收父组件传过来的参数
const acceptParams = (params: ModelProps): void => {
	const teacherId = params.rowData.id;
	if (teacherId) {
		pageData.title = '修改讲师信息';
		Object.assign(pageData.rowData, params.rowData);
	} else {
		pageData.title = '新增讲师信息';
	}
	modelVisible.value = params.visible;
};

const submitData = async () => {
	const validateRes = await formRef.value.validate();
	if (validateRes) {
		return false;
	}
	await saveOrUpdate(pageData.rowData);
	//执行成功的函数
	emit('successFunc');
	resetAndClose();
};
const resetAndClose = () => {
	reset();
	modelVisible.value = false;
};

const reset = () => {
	pageData.rowData = {};
	formRef.value.resetFields();
};
defineExpose({
	acceptParams,
});
</script>

<style scoped lang="less"></style>
